﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Media;
//using NAudio.Wave;//需要NuGet安装NAudio
//using NAudio.Wave.SampleProviders;
namespace vlcplay
{
    public partial class gcs_audio : Form
    {
        public gcs_audio()
        {
            InitializeComponent();
        }
        static int[] C_freq = {0,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1046,1109,1175,1245,1318,1397,1480,1568,1661,1760,1865,1976};
        static int[] C_time = {100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600 };
        // Define a note as a frequency (tone) and the amount of
        // time (duration) the note plays.
        protected struct Note
        {
            int toneVal;
            int durVal;

            // Define a constructor to create a specific note.
            public Note(int frequency, int time) //new Note时可以调用，不能当方法使用
            {
                toneVal = frequency;
                durVal = time;
            }
            public void set_data(int frequency, int time)//方法调用
            {
                toneVal = frequency;
                durVal = time;
            }

            // Define properties to return the note's tone and duration.
            public int NoteTone { get { return toneVal; } }
            public int NoteDuration { get { return durVal; } }
        }
        // Define the frequencies of notes in an octave, as well as
        // silence (rest).
        // Play the notes in a song.
        protected static void C_Play_int(int[,] tune)
        {
            for (int i = 0; i < tune.GetLength(0); i++)
            {
                if (tune[i, 0] == (int)Tone.REST)
                {
                    Thread.Sleep(C_time[tune[i, 1]]);
                }
                else
                    Console.Beep(C_freq[tune[i, 0]], C_time[tune[i, 1]]);
            }
        }
        protected static void C_Play_note(Note[] tune)
        {
            foreach (Note n in tune)
            {
                if (n.NoteTone == (int)Tone.REST)
                    Thread.Sleep(n.NoteDuration);
                else
                    Console.Beep(n.NoteTone, n.NoteDuration);
            }
        }
        // Define the duration of a note in units of milliseconds.
        protected enum Duration
        {
            T1 = 0,
            T2 = 1,
            T3 = 2,
            T4 = 3,
            T5 = 4,
            T6 = 5,
            T7 = 6,
            T8 = 7,
            T9 = 8,
            T10 = 9,
            T11 = 10,
            T12 = 11,
            T13 = 12,
            T14 = 13,
            T15 = 14,
            T16 = 15,
        }
        protected enum Tone
        {
            REST = 0,
            d1      = 1,
            d1sharp = 2,
            d2      = 3,
            d2sharp = 4,
            d3      = 5,
            d4      = 6,
            d4sharp = 7,
            d5      = 8,
            d5sharp = 9,
            d6      = 10,
            d6sharp = 11,
            d7      = 12,
            m1      = 13,
            m1sharp = 14,
            m2      = 15,
            m2sharp = 16,
            m3      = 17,
            m4      = 18,
            m4sharp = 19,
            m5      = 20,
            m5sharp = 21,
            m6      = 22,
            m6sharp = 23,
            m7      = 24,
            u1      = 25,
            u1sharp = 26,
            u2      = 27,
            u2sharp = 28,
            u3      = 29,
            u4      = 30,
            u4sharp = 31,
            u5      = 32,
            u5sharp = 33,
            u6      = 34,
            u6sharp = 35,
            u7      = 36,

        }
        public static void test_audio1()
        {
            // 
            int[,] tow_tiger = {{(int)Tone.m1, (int)Duration.T4 },{(int)Tone.m2, (int)Duration.T4 },{(int)Tone.m3, (int)Duration.T4 },{(int)Tone.m1, (int)Duration.T4 },{(int)Tone.m1, (int)Duration.T4 },
                                {(int)Tone.m2, (int)Duration.T4 },{(int)Tone.m3, (int)Duration.T4 },{(int)Tone.m1, (int)Duration.T4 },{(int)Tone.m3, (int)Duration.T4 },{(int)Tone.m4, (int)Duration.T4 },
                                {(int)Tone.m5, (int)Duration.T8 },{(int)Tone.m3, (int)Duration.T4 },{(int)Tone.m4, (int)Duration.T4 },{(int) Tone.m5, (int) Duration.T8 },{(int)Tone.m5, (int)Duration.T3},
                                {(int)Tone.m6, (int)Duration.T1 },{(int)Tone.m5, (int)Duration.T3 },{(int)Tone.m4, (int)Duration.T1 },{(int)Tone.m3, (int)Duration.T4},{(int)Tone.m1, (int)Duration.T4},
                                {(int)Tone.m5, (int)Duration.T3 },{(int)Tone.m6, (int)Duration.T1 },{(int)Tone.m5, (int)Duration.T3 },{(int)Tone.m4, (int)Duration.T1 },{(int)Tone.m3, (int)Duration.T4 },
                                {(int)Tone.m1, (int)Duration.T4 },{(int)Tone.m2, (int)Duration.T4 },{(int)Tone.d5sharp, (int)Duration.T4 },{(int)Tone.m1, (int)Duration.T8 },{(int)Tone.m2, (int)Duration.T4 },
                                {(int)Tone.d5sharp, (int)Duration.T4 },{(int)Tone.m1, (int)Duration.T8 }
            };

            Note[] c_tow_tiger = new Note[tow_tiger.GetLength(0)];
            for (int i = 0; i < tow_tiger.GetLength(0); i++)
            {
                c_tow_tiger[i].set_data(C_freq[tow_tiger[i, 0]], C_time[tow_tiger[i, 1]]);
            }
        // Play the song
            //C_Play_int(tow_tiger);
            C_Play_note(c_tow_tiger);
        }
        public void Sound_play()
        {
            string outputFile = "output.wav";

            string file = Environment.CurrentDirectory + "\\" + "output.wav";
            SoundPlayer player = new SoundPlayer();//using System.Media;  soundplayer 只能播放 wav文件
            player.SoundLocation = file;
            player.Load();
            player.Play();
        }

        private void button_start_Click(object sender, EventArgs e)
        {
#if false//true //false
            //频率 如果使用单片机，例如262：1/f=T 使用1/262=3816us，故1908us高电平 1908低电平
            //当晶振11.0592MHz  周期未1.085us，需要1758周期，则定时器的技术初始值未65536-1758=63778对应16进制 0xF921；
            int Hz = int.Parse(textBox_Hz.Text);
            int time = int.Parse(textBox_time.Text);
            Console.Beep(Hz, time);

#else
            //Sound_play();
            test_audio1();
#endif

        }
    }
}
